package com.sfh.cp.home.pst;


import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import crm.sfh.server.SMass;
import crm.sfh.server.SUser;
import crm.sfh.server.entity.EMass;
import crm.sfh.server.entity.EUser;
import crm.sfh.server.entity.req.ReqPage;
import crm.sfh.server.entity.rsp.TPage;

public class PstRanking extends AbstractPresenter<IPstRanking.IVRanking> implements IPstRanking {


    @Override
    public void listPage(final ReqPage req) {

        BmobQuery<SUser> query = new BmobQuery<>();
        query.setSkip((req.nowPage - 1) * 10);
        query.setLimit(req.nowPage * 10);
        query.order("-charm");
        query.findObjects(AppCacheManager.getApplication(), new FindListener<SUser>() {
            @Override
            public void onSuccess(List<SUser> list) {
                TPage<EUser> page = new TPage<>();
                page.setNowPage(req.nowPage);
                page.setTotalPage(list.size() >= 10 ? req.nowPage + 1 : req.nowPage);
                List<EUser> masses = new ArrayList<>(list.size());
                for (SUser mass : list) {
                    masses.add(mass.getUser());
                }
                page.setList(masses);
                getView().onSuccess(page);
            }

            @Override
            public void onError(int i, String s) {
                getView().onError(s);
            }
        });
    }
}